package com.qingke.oms.command.shop;

import java.util.List;

import com.qingke.oms.command.ShopCommand;
import com.qingke.oms.pojo.Goods;
import com.qingke.oms.pojo.GoodsStatistic;
import com.qingke.oms.pojo.ShelfRecord;
import com.qingke.oms.pojo.ShelfStatus;
import com.qingke.oms.pojo.Shop;
import com.qingke.oms.pojo.User;
import com.qingke.oms.util.OMSConsole;

public class OffShelfCommand extends ShopCommand implements UpdateGoodsStatistic{

	public OffShelfCommand(User user, Shop shop) {
		super(user, shop);
	}

	@Override
	public void execute() {
		if(user.getId()!=shop.getOwner().getId()){
			OMSConsole.println("It's not your shop,you can't put goods off shelf.");
			return ;
		}
		
		
		OMSConsole.println("list , off shelf or exit :[list],[off],[exit]");
		while(true){
			String cmd=OMSConsole.promptNotEmpty("do>").trim();
			if(cmd.equalsIgnoreCase("list")){
				listGoods();
				continue;
			}
			else if(cmd.equalsIgnoreCase("off")){
				offShelf();
				continue;
			}
			else if(cmd.equalsIgnoreCase("exit")){
				OMSConsole.println("exit off shelf.");
				break ;
			}
			else if(cmd.equalsIgnoreCase("help")){
				OMSConsole.println("- list     list goods the shop have");
				OMSConsole.println("- off      choose goods off shelf");
				OMSConsole.println("- exit     exit on shelf");
				continue;
			}
			else{
				OMSConsole.println("err input.");
				OMSConsole.println("list , off shelf or exit :[list],[off],[exit]");
				continue;
			}
		}
		
		updateGoodsStatistic();
	}
	public void listGoods(){
		List<GoodsStatistic> list=dao.selectGoodsStatisticByShop(shop);
		if(list.size()==0){
			OMSConsole.println("No goods can off shelf");
			return ;
		}
		for(GoodsStatistic gr:list){
			Goods goods=dao.selectGoodsById(gr.getGoods().getId());
			OMSConsole.println("[goods id]:"+gr.getGoods().getId()+",[goods name]"+goods.getName()+",[quantity]:"+gr.getStock());
		}
	}
	public void offShelf(){
		int goodsIdInt=OMSConsole.getInt("goods id:");
		boolean isHave=false;
		shop=dao.selectNewestShopById(shop.getId());
		GoodsStatistic goodsStatistic=null;
		for(GoodsStatistic gs:shop.getGoodsStatistics()){
			if(gs.getGoods().getId()==goodsIdInt){
				goodsStatistic=gs;
				isHave=true;
				break;
			}
		}
		if(!isHave){
			OMSConsole.println("There is no the goods in your shop");
			return ;
		}
		
		
		
		
		int goodsQuantity=OMSConsole.getInt("goods quantity:");
		if(goodsQuantity==0){
			OMSConsole.println("cancel off shelf.");
			return ;
		}
		if(goodsQuantity>goodsStatistic.getStock()){
			OMSConsole.println("The input is more than the quantity of the goods exist.");
			OMSConsole.println("Default the input equal the quantity of the goods exist.");
			OMSConsole.println("[new input]:"+goodsStatistic.getStock());
			goodsQuantity=goodsStatistic.getStock();
		}
		
		ShelfStatus shelfStatus=dao.selectShelfStatusByStatus("off shelf");
		ShelfRecord shelfRecord=new ShelfRecord();
		shelfRecord.setShop(shop);
		shelfRecord.setGoods(goodsStatistic.getGoods());
		shelfRecord.setQuantity(goodsQuantity);
		shelfRecord.setShelfStatus(shelfStatus);
		
		boolean isSuccess=dao.insertShelfRecord(shelfRecord);
		if(isSuccess){
			updateGoodsStatistic();
			OMSConsole.println("Off shelf success.");
		}
		else
			OMSConsole.println("Off shelf failed.");
		
		
	}

}
